package com.ludomancy.archeologyrpg.entities;

import java.awt.Graphics;
import java.awt.Point;

import com.ludomancy.archeologyrpg.GameTheme;
import com.ludomancy.archeologyrpg.Entity;
import com.ludomancy.archeologyrpg.Game;

public class Generator extends Entity
{
	private static final int maxPowerup = 100;
	private static final int timeFromPoweredToNonpowered = Game.discretize(3000);
	private int powerupRate = 2;
	private int currentTimeToNonpowered = 0;
	private int currentPowerup = 0;
	
	public Generator(Point p)
	{
		super(p);
	}

	public void paint(Graphics g, int x, int y)
	{
		if(isPowered())
		{
			paintDefaultIcon(g, GENERATOR_ACTIVE, x, y);
		}
		else
		{
			paintDefaultIcon(g, GENERATOR_INACTIVE, x, y);
		}
	}
	
	public boolean isPowered()
	{
		return currentTimeToNonpowered != 0;
	}

	public void update(boolean running, boolean activated)
	{
		if(running)
		{
			if(activated)
			{
				if(!isPowered())
				{
					currentPowerup += powerupRate;
					currentPowerup = Math.min(maxPowerup, currentPowerup);
					if(currentPowerup == maxPowerup)
					{
						// powered up!
						currentTimeToNonpowered = timeFromPoweredToNonpowered;
					}
				}
			}
			else
			{
				if(isPowered())
				{
					currentTimeToNonpowered --;
				}
				else
				{
					currentPowerup -= powerupRate*2;
					currentPowerup = Math.max(0, currentPowerup);
				}
			}
		}
	}

	public void paintInfo(Graphics g)
	{
		g.setColor(GameTheme.theme.getDefaultTextColor());
		g.setFont(GameTheme.theme.tinyFont);
		if(!isPowered())
		{
			GameTheme.theme.paintBar(g, px, py+Game.ENTITY_TILE_SIZE, currentPowerup, maxPowerup);
		}
	}

	public String getActionVerb()
	{
		return "Activate";
	}
}
